package com.google.code.avajhelpdesk.pages.users;

import org.apache.wicket.authorization.strategies.role.annotations.AuthorizeInstantiation;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.basic.MultiLineLabel;
import org.apache.wicket.markup.html.link.Link;

import com.google.code.avajhelpdesk.entities.User;
import com.google.code.avajhelpdesk.model.GenericLoadableDetachableModel;
import com.google.code.avajhelpdesk.pages.BasePage;

@AuthorizeInstantiation({User.ROLE_ADMIN, User.ROLE_USER})
public class UserProfileView extends BasePage {
	
	private GenericLoadableDetachableModel<User> detachedUser;
	
	public UserProfileView(User user) {
		detachedUser = new GenericLoadableDetachableModel<User>(user);
		
		add(new Label("username", user.getUsername()));
		add(new Label("firstname", user.getFirstName()));
		add(new Label("lastname", user.getLastName()));
		add(new MultiLineLabel("contacts", user.getContactInfo()));
		
		boolean allowEdit = (user.equals(getCurrentUser()) || user.getRoles().contains(User.ROLE_ADMIN));
		
		Link editLink = new Link("editLink") {
			@Override
			public void onClick() {
				User user = detachedUser.getObject();
				redirectToInterceptPage(new UserProfileEdit(user));
			}
		};
		
		editLink.setVisible(allowEdit);
		add(editLink);
	}

	@Override
	protected String getPageTitle() {
		return "View user profile";
	}
}
